package q232_MyQueue;

import java.util.Deque;
import java.util.LinkedList;

/*
实际上就是利用两个栈
栈1只需要不断push压入
而每次要弹出时 如果栈2有元素 就直接弹出
否则就将所有栈1的弹出 并压入到栈2中 实现了先进先出
 */
public class MyQueue {
    Deque<Integer> stack_1;
    Deque<Integer> stack_2;
    public MyQueue() {
        stack_1 = new LinkedList<>();
        stack_2 = new LinkedList<>();
    }

    public void push(int x) {
        stack_1.push(x);
    }

    public int pop() {
        if (!stack_2.isEmpty()) return stack_2.pop();

        while (!stack_1.isEmpty()) stack_2.push(stack_1.pop());
        return stack_2.pop();
    }

    public int peek() {
        if (!stack_2.isEmpty()) return stack_2.peek();
        while (!stack_1.isEmpty()) stack_2.push(stack_1.pop());
        return stack_2.peek();
    }

    public boolean empty() {
        return stack_1.isEmpty() && stack_2.isEmpty();
    }
}
